package com.engine.cese.duss.srm.needpool.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.srm.util.CalUtil;
import com.engine.cese.duss.srm.util.DataUtil;
import com.engine.cese.duss.srm.util.GenUtil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-09
 * @Description: 物料采购需求池查询列表
 * @ModifyLog:
 **/

public class WeaNeedPoolAction {

    /**
     * @Description: 需求关闭
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/reqCloseData")
    @Produces({MediaType.TEXT_PLAIN})
    public String reqCloseData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String ids = Util.null2String( request.getParameter( "ids" ) );
            String cglx = Util.null2String( request.getParameter( "cglx" ) );
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            String cgxqTab = GenUtil.getCgxqTableType( cglx );
            rs.executeQuery( "select id,jbgsl,cgms from " + cgxqTab + " where id in (" + ids + ")" );
            int count1 = 0;
            int count2 = 0;
            while (rs.next()) {
                String id = Util.null2String( rs.getString( "id" ) );//ID
                String jbgsl = Util.null2String( rs.getString( "jbgsl" ) );//仅变更数量
                String cgms = Util.null2String( rs.getString( "cgms" ) );//	采购模式

                //①如果“仅变更数量”=是，且明细表中“是否已签合同”全部=是，可直接关闭该需求；
                String sql2 = "select count(1) from " + cgxqTab + "_dt1 where sfyqht=1 and mainid=?";
                rs1.executeQuery( sql2, id );
                if (rs1.next()) {
                    int count = Util.getIntValue( rs1.getString( 1 ) );
                    if ("0".equals( jbgsl ) && count == 0) {
                        rs1.executeUpdate( "update " + cgxqTab + " set fplx='2' where id =?", id );
                        count1++;
                    }
                }
                //②如果“采购模式”=公司集中采购，且明细表中“不需招标”全部勾选的，可直接关闭该需求；
                String sql1 = "select count(1) from " + cgxqTab + "_dt1 where bxzb=0 and mainid=?";
                rs1.executeQuery( sql1, id );
                if (rs1.next()) {
                    int count = Util.getIntValue( rs1.getString( 1 ) );
                    if ("0".equals( cgms ) && count == 0) {
                        rs1.executeUpdate( "update " + cgxqTab + " set fplx='2' where id =?", id );
                        count2++;
                    }
                }
            }
            apidatas.put( "count1", count1 );
            apidatas.put( "count2", count2 );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 直接分配
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getZjfpData")
    @Produces({MediaType.TEXT_PLAIN})
    public String getZjfpData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String userid = Util.null2String( request.getParameter( "userid" ) );
            String ids = Util.null2String( request.getParameter( "ids" ) );
            String cglx = Util.null2String( request.getParameter( "cglx" ) );
            RecordSet rs = new RecordSet();
            rs.writeLog( "userid="+userid+",ids="+ids+",cglx="+cglx );
            String cgxqTab = GenUtil.getCgxqTableType( cglx );
            String sql = "select id,cgy from " + cgxqTab + " where id in (" + ids + ")";
            rs.executeQuery( sql );
            int counts = rs.getCounts();
            int resultCount = 0;
            int count = 0;
            while (rs.next()) {
                String id = Util.null2String( rs.getString( "id" ) );
                String cgy = Util.null2String( rs.getString( "cgy" ) );
                if (!"".equals( cgy )) {
                    switch (cglx) {
                        case "0":
                            if (DataUtil.wlneedTowltaskPool( userid, id )) {
                                rs.executeUpdate( "update " + cgxqTab + " set fplx='1' where id in(" + id + ")" );
                                resultCount++;
                            }
                            break;
                        case "1":
                            if (DataUtil.gcneedTogctaskPool( userid, id )) {
                                rs.executeUpdate( "update " + cgxqTab + " set fplx='1' where id in(" + id + ")" );
                                resultCount++;
                            }
                            break;
                        case "2":
                            if (DataUtil.zlneedTozltaskPool( userid, id )) {
                                rs.executeUpdate( "update " + cgxqTab + " set fplx='1' where id in(" + id + ")" );
                                resultCount++;
                            }
                            break;
                        case "3":
                            if (DataUtil.ysneedToystaskPool( userid, id )) {
                                rs.executeUpdate( "update " + cgxqTab + " set fplx='1' where id in(" + id + ")" );
                                resultCount++;
                            }
                            break;
                        case "4":
                            if (DataUtil.fwneedTofwtaskPool( userid, id )) {
                                rs.executeUpdate( "update " + cgxqTab + " set fplx='1' where id in(" + id + ")" );
                                resultCount++;
                            }
                            break;
                        default:
                    }

                } else {
                    count++;
                }
            }
            rs.writeLog( "---------分配需求总数量=" + counts );
            rs.writeLog( "---------分配需求完成数量=" + resultCount );
            apidatas.put( "count", count );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取资格预审通过的供应商数量
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/generateBglsh")
    @Produces({MediaType.TEXT_PLAIN})
    public String generateBglsh(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cglx = Util.null2String( request.getParameter( "cglx" ) );
            String xqly = Util.null2String( request.getParameter( "xqly" ) );
            String xmmc = Util.null2String( request.getParameter( "xmmc" ) );
            String xqbh = Util.null2String( request.getParameter( "xqbh" ) );
            RecordSet rs = new RecordSet();
            String cgxqTable = GenUtil.getCgxqTableType( cglx );
            String newbglsh = "";
            String sql2 = "select max(to_number(nvl(bglsh,0))) from " + cgxqTable + " where xqly=? and xmmc=? and xqbh=?";
            rs.executeQuery( sql2, xqly, xmmc, xqbh );
            rs.next();
            newbglsh = CalUtil.fAdd( rs.getString( 1 ), "1" );
            String sql3 = "update " + cgxqTable + " set bglsh=? where xqly=? and xmmc=? and xqbh=?";
            rs.executeUpdate( sql3, newbglsh, xqly, xmmc, xqbh );
            apidatas.put( "bglsh", newbglsh );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 物料需求拆分
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getSplitDemand")
    @Produces({MediaType.TEXT_PLAIN})
    public String getSplitDemand(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cfdid = Util.null2String( request.getParameter( "cfdid" ) );
            String checkids = Util.null2String( request.getParameter( "checkids" ) );
            List<String> list = Arrays.asList( checkids.split( "," ) );
            RecordSet rs = new RecordSet();
            for (String checkid : list) {
                rs.executeUpdate( "update uf_cgxq_wl_dt1 set cfbj=1,cfdid=? where id =?", cfdid, checkid );
            }
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取物料需求单id
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getWlxqdid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWlxqdid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String billid = Util.null2String( request.getParameter( "billid" ) );
            RecordSet rs = new RecordSet();
            rs.executeQuery( "select xqdid from uf_xqcf_wl where id=?", billid );
            rs.next();
            String xqdid = Util.null2String( rs.getString( "xqdid" ) );
            apidatas.put( "xqdid", xqdid );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
}
